package com.dailyblue.java.boot.mybatisplus.test;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dailyblue.java.boot.mybatisplus.BootMyBatisPlusApplication;
import com.dailyblue.java.boot.mybatisplus.bean.School;
import com.dailyblue.java.boot.mybatisplus.bean.User;
import com.dailyblue.java.boot.mybatisplus.service.SchoolService;
import com.dailyblue.java.boot.mybatisplus.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest(classes = BootMyBatisPlusApplication.class)
@Slf4j
public class App {
    @Resource
    private UserService userService;
    @Resource
    private SchoolService schoolService;

    @Test
    public void a() {
        log.info("开始测试....");
        // 获取user表的所有数据
        List<User> list = userService.list();
        log.info("所有数据是：{}", list);
    }

    @Test
    public void b() {
        log.info("开始删除");
        userService.removeById(4);
    }

    // 运行会出错
    @Test
    public void c() {
        School school = new School();
        school.setLoc("xxxx");
        schoolService.save(school);
    }

    @Test
    public void d() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("job", "abc");
        wrapper.ne("job", "bbb");
        wrapper.gt("birthday", "2000-1-1");
        wrapper.ge("birthday", "2003-2-2");
        wrapper.lt("id", 34);
        wrapper.le("id", 99);
        userService.list(wrapper);
    }

    @Test
    public void e1() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.between("birthday", "2000-1-1", "2000-12-31");
        userService.list(wrapper);
    }

    @Test
    public void e2() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("nickname", "王");
        wrapper.notLike("nickname", "王");
        wrapper.likeLeft("nickname", "王");
        wrapper.likeRight("nickname", "王");
        wrapper.isNull("job");
        userService.list(wrapper);
    }

    @Test
    public void e3() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.in("job","aa","bb","cc");
        userService.list(wrapper);
    }
    @Test
    public void e4() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.inSql("schoolno","select id from school where id<3");
        userService.list(wrapper);
    }

    @Test
    public void e5(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.groupBy("id","userCode");
        userService.list(wrapper);
    }
    @Test
    public void e6(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        // 查询哪些字段
        wrapper.select("job","count(id)","8");
        // 按照那个字段分组
        wrapper.groupBy("job");
        userService.list(wrapper);
    }
    @Test
    public void e7(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("job");
        wrapper.orderByDesc("birthday");
        userService.list(wrapper);
    }

    @Test
    public void e8(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper
                .eq("job","aaa")
                .or()
                .lt("birthday","2003-2-1");

        userService.list(wrapper);
    }
    @Test
    public void f(){
        userService.find1();
    }
}
